
package UserInterface;

/**
 * Animates the menu so the user can see when the cursor has changed the position or select an option
 * @author Heldraug
 */
public class MenuMotion implements Runnable{

    private Thread thread;
    private Menu menu;
    private final int DELAY = 1000/30;
    private final int DELAY_LOGOS = 1500;
    private boolean runningStatus;
    //private int actualFrames;
    //private long lastSystemTime;

    /**
     * Creates a MenuMotion object containing a reference to its Menu container.
     * @param menu the menu container
     */
    public MenuMotion(Menu menu){
        this.menu = menu;
    }
/*
    public void calculateFPS() {
        if(System.currentTimeMillis() - lastSystemTime > 1000) {
            lastSystemTime = System.currentTimeMillis();
            menu.setFPS(actualFrames);
            actualFrames = 0;
        } else {
            actualFrames++;
        }
    }
 *
 */

    /**
     * Refreshes the screen's content, according to the information contained within Menu.
     * @see readKeyboard;
     * @see paintCommon();
     * @see paint();
     * @throws InterruptedException It may only be thrown if something goes wrong. The application is built and is already tested to run
     * without exceptions being thrown.
     */
    public void run() {
        while(runningStatus){
            //this.calculateFPS();
            menu.readKeyboard();
            menu.paintCommon();
            //menu.paintFPS();
            menu.paint();
            try {
                Thread.sleep(DELAY);
            } catch (InterruptedException ex) {}
        }
    }

    /**
     * Creates the new Thread and sets it to run immediately.
     */
    public void begin() {
        thread = new Thread(this);
        this.runningStatus = true;
        thread.start(); //Llama al método run()
    }

    /**
     * Stops the Thread's cycle contained in the run() method.
     * @see run();
     */
    public void stop() {
        runningStatus = false;
    }

    /**
     * Destroys the Thread when the Menu's shutdown sequence is called.
     */
    public void shutDown() {
        runningStatus = false;
        thread = null;
        if(runningStatus == false && thread == null && menu == null)
            System.out.println("Thread succesfully destroyed.");
    }

}